<style>
    #obj-add {
        padding: 20px 25px 25px 0;
    }
    #obj-add .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="obj-add" lay-title="业务数据上报">
    <div class="layui-inline">
        <label class="layui-form-label">上传日期</label>
        <div class="layui-input-inline">
            <input type="text" name="date" id="febs-form-group-date" lay-verify="date"
                   placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <form class="layui-form" action="" lay-filter="obj-add-form" id="items_form">
        <table class="layui-table" id="items_table">
            <colgroup>
                <col width="150">
                <col width="200">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
                <col width="100">
            </colgroup>
            <thead>
            <tr>
                <th>单位名称</th>
                <th>主项</th>
                <th>子项</th>
                <th>事项类别</th>
                <th>业务办理名称</th>
                <th>咨询业务量</th>
                <th>邮寄业务量</th>
                <th>线上业务量</th>
                <th>线下业务量-大厅</th>
                <th>线下业务量-分厅</th>
            </tr>
            </thead>
            <tbody id="items_table_tbody">
            </tbody>
        </table>

        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="obj-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>

<script>
    layui.use(['febs', 'form', 'formSelects', 'validate', 'treeSelect', 'laydate'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            formSelects = layui.formSelects,
            laydate = layui.laydate,
            treeSelect = layui.treeSelect,
            form = layui.form,
            $view = $('#obj-add'),
            validate = layui.validate;

        form.verify(validate);
        form.render();
        laydate.render({
            elem: '#febs-form-group-date'
        });
        formSelects.render();
        initTable();

        form.on('submit(obj-add-form-submit)', function (data) {
            if($("#febs-form-group-date").val()==''){
                febs.alert.success('请选择日期');
                return false;
            }
            /*febs.post(ctx + 'hallUpData', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增成功');
                $('#febs-obj').find('#query').click();
            });
            return false;*/
            var zxywl=0;//咨询业务量
            var yjywl=0;//邮寄业务量
            var tr_data='';//行数据，多行用逗号分隔开
            $('#items_table tr').each(function(i){          // 遍历 tr
                if(i>=1){
                    console.info("第"+i+"行");
                    var id=0;
                    var xsywl=0;//线上业务量
                    var xxywl_dt=0;//线下业务量_大厅
                    var xsywl_ft=0;//线下业务量_分厅

                    id=$(this).children('td:eq(0)').children("input:eq(0)").val();
                    if(i==1){
                        zxywl=$(this).children('td:eq(5)').children("input:eq(0)").val();
                        yjywl=$(this).children('td:eq(6)').children("input:eq(0)").val();

                        xsywl=$(this).children('td:eq(7)').children("input:eq(0)").val();
                        xxywl_dt=$(this).children('td:eq(8)').children("input:eq(0)").val();
                        xsywl_ft=$(this).children('td:eq(9)').children("input:eq(0)").val();

                        tr_data+=id+'_'+xsywl+'_'+xxywl_dt+'_'+xsywl_ft;
                    }else{
                        xsywl=$(this).children('td:eq(5)').children("input:eq(0)").val();
                        xxywl_dt=$(this).children('td:eq(6)').children("input:eq(0)").val();
                        xsywl_ft=$(this).children('td:eq(7)').children("input:eq(0)").val();
                        tr_data+=','+id+'_'+xsywl+'_'+xxywl_dt+'_'+xsywl_ft;
                    }
                }
            });
            var pushObj={};
            pushObj.tr_data=tr_data;
            pushObj.answer_count=zxywl;
            pushObj.post_count=yjywl;
            pushObj.buss_time=$("#febs-form-group-date").val();
            febs.post(ctx + 'pushData', pushObj, function () {
                layer.closeAll();
                febs.alert.success('数据上传成功');
                $('#febs-obj').find('#query').click();
            });
            return false;
        });

        function initTable(){
            febs.get(ctx + 'hallItem', null, function (data) {
                if(data.code==200){
                    var d=data.data;
                    var tbodyHtml='';
                    for(var i=0;i<d.length;i++){
                        console.info("aaa");
                            console.info(d[i]);
                            tbodyHtml+='<tr>';
                            tbodyHtml+='<td>'+d[i].departName+'<input type="hidden" value="'+d[i].id+'" /></td>';
                            tbodyHtml+='<td>'+d[i].mainItemName+'</td>';
                            tbodyHtml+='<td>'+d[i].sumItemName+'</td>';
                            tbodyHtml+='<td>'+d[i].itemType+'</td>';
                            tbodyHtml+='<td>'+d[i].name+'</td>';
                            if(i==0){
                            tbodyHtml+='<td rowspan="'+d.length+'"><input type="text" value="0" style="width:50px"/></td>';
                            tbodyHtml+='<td rowspan="'+d.length+'"><input type="text" value="0" style="width:50px"/></td>';
                        }
                        tbodyHtml+='<td ><input type="text" value="0" style="width:50px"/></td>';
                        tbodyHtml+='<td ><input type="text" value="0" style="width:50px"/></td>';
                        tbodyHtml+='<td ><input type="text" value="0" style="width:50px"/></td>';
                        tbodyHtml+='</tr>';
                    }
                    $("#items_table_tbody").html(tbodyHtml);
                }
            });
        }
    });
</script>